<template>
  <div>
    <el-button style="float: right; margin-bottom: 10px" type="primary" plain icon="el-icon-plus"
      @click="handleAdd">新增</el-button>
    <el-table border="" v-loading="loading" :data="managerInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="表名称" align="center" prop="monitorTableType">
        <template slot-scope="scope">
          <span>{{
            scope.row.monitorTableType == 0
              ? "A表 监测信息表"
              : "B表 药品不良反应/事件报告表"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入组日期" align="center" prop="inputFormDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inputFormDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleLook(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" :disabled="isDisabled(scope.row)"
            @click="handleDown(scope.row)">下载</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改电子监测管理信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表名称" prop="monitorTableType">
          <el-select style="width: 100%" v-model="form.monitorTableType" placeholder="请选择表类型">
            <el-option v-for="option in tableOptions" :key="option.value" :label="option.label"
              :value="option.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManagerInfo,
  getManagerInfo,
  delManagerInfo,
  addManagerInfo,
  updateManagerInfo,
  delManagerInfoB,
} from "@/api/workstation/managerInfo";

export default {
  name: "ManagerInfo",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子监测管理信息表格数据
      managerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        medicalRecordNo: this.$route.query.medicalRecordNo || undefined,
        monitorId: undefined,
        monitorRecordNo: undefined,
        monitorTableType: undefined,
        inputFormDate: undefined,
        isInputForm: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
        medicalRecordNo: [
          { required: true, message: "患者流水号不能为空", trigger: "blur" },
        ],
        monitorId: [{ required: true, message: "监测表id不能为空", trigger: "blur" }],
        monitorRecordNo: [
          { required: true, message: "监测表流水号不能为空", trigger: "blur" },
        ],
        monitorTableType: [
          { required: true, message: "表名称不能为空", trigger: "change" },
        ],
        inputFormDate: [{ required: true, message: "入组日期不能为空", trigger: "blur" }],
        isInputForm: [{ required: true, message: "是否已入组不能为空", trigger: "blur" }],
      },
      tableOptions: [
        { label: "A表 监测信息表", value: 0 }, // 前台显示 0A表 监测信息表，后台传递 0
        { label: "B表 药品不良反应/事件报告表", value: 1 }, // 前台显示 1B表 药品不良反应/事件报告表，后台传递 1
      ],
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    isDisabled(row) {
      if (row.inputFormDate && row.monitorRecordNo) {
        return false
      } else {
        return true
      }
    },
    /** 查询电子监测管理信息列表 */
    getList(id,medicalRecordNo) {
      this.loading = true;
      this.queryParams.medicalRecordNo = this.$route.query.medicalRecordNo || medicalRecordNo;
      listManagerInfo(this.queryParams).then((response) => {
        this.managerInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        medicalRecordNo: undefined,
        monitorId: undefined,
        monitorRecordNo: undefined,
        monitorTableType: undefined,
        inputFormDate: undefined,
        isInputForm: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleDown() {
      // // 假设Word文档的URL是固定的或者从data中获取
      // const fileUrl = 'F:\新建文件夹\clinicalpharmacistworkstation\src\views\electronicDetectionForm\components\\Aform.pdf'; // 请替换为实际文档URL
      // // 创建隐藏的a标签并触发点击下载
      // const link = document.createElement('a');
      // link.href = fileUrl;
      // link.download = '附件01舒血宁注射液（雪乐通）监测表-A'; // 自定义下载文件名
      // document.body.appendChild(link);
      // link.click();
      // document.body.removeChild(link); // 下载后移除元素
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增监测表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.loading = true;
      // this.reset();
      // const id = row.id || this.ids;
      // getManagerInfo(id).then((response) => {
      //   this.loading = false;
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改电子监测管理信息";
      // });

      // 触发事件通知父组件需要切换到 A 还是 B，并传递id和medicalRecordNo参数
      this.$emit("switch-tab", {
        name: row.monitorTableType == 0 ? "first" : "second",
        id: row.id,
        medicalRecordNo: row.medicalRecordNo,
      });
      // window.open(`
      // /electronicDetectionForm?id=${row.id}&medicalRecordNo=${row.medicalRecordNo}
      // `);
    },
    // 查看按钮
    handleLook(row) {
      this.$emit("switch-tab", {
        name: row.monitorTableType == 0 ? "first" : "second",
        id: row.id,
        medicalRecordNo: row.medicalRecordNo,
        isLook: true,
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.medicalRecordNo = this.$route.query.medicalRecordNo;
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateManagerInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addManagerInfo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除电子监测管理信息编号为"' +
          (row.monitorTableType == 0
            ? "A表 监测信息表"
            : "B表 药品不良反应/事件报告表") +
          '"的数据项？'
        )
        .then(() => {
          this.loading = true;
          if (row.monitorTableType == 0) {
            // 删除A表接口
            return delManagerInfo(ids);
          } else {
            // 删除B表接口
            return delManagerInfoB(ids);
          }
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "workstation/managerInfo/export",
        {
          ...this.queryParams,
        },
        `managerInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
